﻿using KYJBLL;
using KYJModels;
using KYJScadaProModbusRtu;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ToolsLib;

namespace KYJScadaProModbusRTU
{
    public partial class FrmUserManage : Form
    {
        public FrmUserManage()
        {
            InitializeComponent();

            this.dgv_Main.AutoGenerateColumns = false;

            UpdateSysAdminList();

            if (sysAdmins.Count > 0)
            {
                UpdateSysAdminInfo(sysAdmins[0]);
            }
        }

        /// <summary>
        /// 当前用户中所有的用户集合
        /// </summary>
        private List<SysAdmin> sysAdmins = new List<SysAdmin>();

        /// <summary>
        /// 用户管理的业务对象
        /// </summary>
        private SysAdminManage sysAdminManage = new SysAdminManage();

        /// <summary>
        /// 更新用户集合
        /// </summary>
        private void UpdateSysAdminList()
        {
            try
            {
                sysAdmins = sysAdminManage.QuerySysAdmins();

                sysAdmins.ForEach(c => c.LoginPwd = StringSecurityHelper.DESDecrypt(c.LoginPwd));

                this.dgv_Main.DataSource = null;
                this.dgv_Main.DataSource = sysAdmins;

            }
            catch (Exception ex)
            {
                new FrmMessageBoxWithoutAck("查询失败：" + ex.Message, "用户查询失败").ShowDialog();
            }
        }
        private void UpdateSysAdminInfo(SysAdmin sysAdmin)
        {
            if(sysAdmin != null)
            {
                this.txt_LoginName.Text = sysAdmin.LoginName;
                this.txt_LoginPwd.Text = sysAdmin.LoginPwd;
                this.txt_LoginPwd2.Text = sysAdmin.LoginPwd;

                this.chk_logAlarm.Checked = sysAdmin.LogAlarm;
                this.chk_HistoryLog.Checked = sysAdmin.HistoryLog;
                this.chk_ActualTrent.Checked = sysAdmin.ActualTrend;
                this.chk_HistoryTrent.Checked = sysAdmin.HistoryTrend;
                this.chk_Paramset.Checked = sysAdmin.ParamSet;
                this.chk_Report.Checked = sysAdmin.Report;
                this.chk_UserManage.Checked = sysAdmin.UserManage;
            }
        }

        private void dgv_Main_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                int i = this.dgv_Main.SelectedRows[0].Index;
                UpdateSysAdminInfo(sysAdmins[this.dgv_Main.SelectedRows[0].Index]);
            }
        }

        private void dgv_Main_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //显示序列号
            DataGridViewHelper.DgvRowPostPaint(this.dgv_Main, e);
        }

        private void dgv_Main_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 2)
            {
                string value = this.dgv_Main.Rows[e.RowIndex].Cells[e.ColumnIndex].Value?.ToString();
                if (value == "True")
                {
                    e.Value = "启用";
                }
                else
                {
                    e.Value = "禁用";
                }
            }
        }


        #region 添加用户
        private void btn_Add_Click(object sender, EventArgs e)
        {
            //数据验证
            if (this.txt_LoginName.Text.Length == 0)
            {
                new FrmMessageBoxWithoutAck("用户名不能为空", "添加用户").ShowDialog();
                return;
            }
            if (this.txt_LoginPwd.Text.Length == 0)
            {
                new FrmMessageBoxWithoutAck("用户密码不能为空", "添加用户").ShowDialog();
                return;
            }
            if (this.txt_LoginPwd2.Text.Length == 0)
            {
                new FrmMessageBoxWithoutAck("确认密码不能为空", "添加用户").ShowDialog();
                return;
            }
            if (this.txt_LoginPwd.Text != this.txt_LoginPwd2.Text)
            {
                new FrmMessageBoxWithoutAck("两次输入密码不一致", "添加用户").ShowDialog();
                return;
            }
            if (sysAdmins.Where(c => c.LoginName == this.txt_LoginName.Text.Trim()).ToList().Count > 0)
            {
                new FrmMessageBoxWithoutAck("该用户名已经存在", "添加用户").ShowDialog();
                return;
            }

            //组织对象
            SysAdmin sysAdmin = new SysAdmin()
            {
                LoginName = this.txt_LoginName.Text.Trim(),
                LoginPwd = StringSecurityHelper.DESEncrypt(this.txt_LoginPwd.Text.Trim()),
                LogAlarm = this.chk_logAlarm.Checked,
                HistoryLog = this.chk_HistoryLog.Checked,
                ActualTrend = this.chk_ActualTrent.Checked,
                HistoryTrend = this.chk_HistoryTrent.Checked,
                ParamSet = this.chk_Paramset.Checked,
                Report = this.chk_Paramset.Checked,
                UserManage = this.chk_UserManage.Checked
            };

            //调用方法
            try
            {
                if (sysAdminManage.AddSysAdmin(sysAdmin) == 1)
                {
                    UpdateSysAdminList();
                }
                else
                {
                    new FrmMessageBoxWithoutAck("添加用户失败", "添加用户").ShowDialog();

                }
            }
            catch (Exception ex)
            {
                new FrmMessageBoxWithoutAck("添加用户失败:" + ex.Message, "添加用户").ShowDialog();
            }
        }
        #endregion

        #region  修改用户
        private void btn_Modify_Click(object sender, EventArgs e)
        {
            //数据验证
            if (this.txt_LoginName.Text.Length == 0)
            {
                new FrmMessageBoxWithoutAck("用户名不能为空", "修改用户").ShowDialog();
                return;
            }
            if (this.txt_LoginPwd.Text.Length == 0)
            {
                new FrmMessageBoxWithoutAck("用户密码不能为空", "修改用户").ShowDialog();
                return;
            }
            if (this.txt_LoginPwd2.Text.Length == 0)
            {
                new FrmMessageBoxWithoutAck("确认密码不能为空", "修改用户").ShowDialog();
                return;
            }
            if (this.txt_LoginPwd.Text != this.txt_LoginPwd2.Text)
            {
                new FrmMessageBoxWithoutAck("两次输入密码不一致", "修改用户").ShowDialog();
                return;
            }

            //如果修改用户名就需要进行判断用户名是否存在
            if (sysAdmins[this.dgv_Main.SelectedRows[0].Index].LoginName != this.txt_LoginName.Text)
            {
                if (sysAdmins.Where(c => c.LoginName == this.txt_LoginName.Text.Trim()).ToList().Count > 0)
                {
                    new FrmMessageBoxWithoutAck("该用户名已经存在", "修改用户").ShowDialog();
                    return;
                }
            }

            //封装数据
            SysAdmin sysAdmin = new SysAdmin()
            {
                LoginId = sysAdmins[this.dgv_Main.SelectedRows[0].Index].LoginId,
                LoginName = this.txt_LoginName.Text.Trim(),
                LoginPwd = StringSecurityHelper.DESEncrypt(this.txt_LoginPwd.Text.Trim()),
                LogAlarm = this.chk_logAlarm.Checked,
                HistoryLog = this.chk_HistoryLog.Checked,
                ActualTrend = this.chk_ActualTrent.Checked,
                HistoryTrend = this.chk_HistoryTrent.Checked,
                ParamSet = this.chk_Paramset.Checked,
                Report = this.chk_Paramset.Checked,
                UserManage = this.chk_UserManage.Checked
            };
            //调用方法
            try
            {
                if (sysAdminManage.ModifySysAdmin(sysAdmin) == 1)
                {
                    UpdateSysAdminList();
                }
                else
                {
                    new FrmMessageBoxWithoutAck("修改用户失败", "修改用户").ShowDialog();
                }
            }
            catch (Exception ex)
            {
                new FrmMessageBoxWithoutAck("修改用户失败:" + ex.Message, "修改用户").ShowDialog();
            }
        }
        #endregion

        #region 删除用户
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            int loginId = sysAdmins[this.dgv_Main.SelectedRows[0].Index].LoginId;
            string loginName = sysAdmins[this.dgv_Main.SelectedRows[0].Index].LoginName;
            try
            {
                if (new FrmMessageBoxWithAck($"是否删除{loginName}用户！", "删除提示").ShowDialog() == DialogResult.OK)
                {
                    if (sysAdminManage.DeleteSysAdmin(loginId) == 1)
                    {
                        UpdateSysAdminList();
                    }
                    else
                    {
                        new FrmMessageBoxWithoutAck("删除用户失败", "修改用户").ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {

                new FrmMessageBoxWithoutAck("删除用户失败:" + ex.Message, "修改用户").ShowDialog();
            }

        }
        #endregion

        #region 清空信息
        private void btn_Clear_Click(object sender, EventArgs e)
        {
            this.txt_LoginName.Clear();
            this.txt_LoginPwd.Clear();
            this.txt_LoginPwd2.Clear();
            SetChecked(false);
        }
        #endregion

        private void SetChecked(bool value)
        {
            this.chk_logAlarm.Checked = value;
            this.chk_HistoryLog.Checked = value;
            this.chk_ActualTrent.Checked = value;
            this.chk_HistoryTrent.Checked = value;
            this.chk_Paramset.Checked = value;
            this.chk_Paramset.Checked = value;
            this.chk_Report.Checked = value;
            this.chk_UserManage.Checked = value;
        }

        #region 全选/不选
        private void btn_SelectAll_Click(object sender, EventArgs e)
        {
            SetChecked(!this.chk_Paramset.Checked);
        }
        #endregion

        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parss = base.CreateParams;
                parss.ExStyle |= 0x02000000;
                return parss;
            }
        }
        #endregion
    }
}
